package ajax;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet("/ajaxjson1get")
public class AjaxJson1get extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        resp.setContentType("application/json");
//        1.获取前端以get方式传过来的字符串
        String data = req.getParameter("data");
//        2.json字符串转成json对象
        JSONObject jsonObject = JSONObject.parseObject(data);
//        3.获取json对象中的每一个数据
        String name1 = jsonObject.getString("name1");
        String pwd1 = jsonObject.getString("pwd1");
        System.out.println("name1 = " + name1);
        System.out.println("pwd1 = " + pwd1);
        PrintWriter writer = resp.getWriter();
        writer.print(data);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
